package com.rfid.afd.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rfid.afd.activity.view.ILoginView;
import com.rfid.afd.presenter.LoginPresenter;
import com.rfid.rls_psw.App;
import com.rfid.rls_psw.R;
import com.rfid.util.Util;

import java.util.Observable;
import java.util.Observer;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends AppCompatActivity implements ILoginView, Observer {
    private LoginPresenter mLoginPresenter;

    @BindView(R.id.userName)
    EditText mUserName;
    @BindView(R.id.password)
    EditText mPassWord;
    @BindView(R.id.versionName)
    TextView mVersionName;
    private ProgressDialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mLoginPresenter = new LoginPresenter(this);
        App.getInstance().getObservable().addObserver(this);
        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            App app = (App) getApplication();
            app.init();
            mLoginPresenter.checkOrdownloadDBFile();
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }
        try {
            mVersionName.setText("版本：" + Util.getVersionName());
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            App app = (App) getApplication();
            app.init();
            mLoginPresenter.checkOrdownloadDBFile();
        } else {
            Toast.makeText(this, "无法获取文件读写权限", Toast.LENGTH_LONG).show();
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getInstance().getObservable().deleteObserver(this);
    }

    public void login(View view) {
        mLoginPresenter.login(mUserName.getText().toString(),mPassWord.getText().toString(),false,null);
    }

    public void quit(View view) {
        finish();
    }

    @Override
    public void onLoginSuccess(boolean online) {
        HomeActivity.startActivity(this);
    }

    @Override
    public void onLoginFail(String reason) {
        Toast.makeText(this,reason,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDBPreparedSuccess() {
        App.getInstance().initDb();
    }

    @Override
    public void onDBPreparedFail() {
        App.getInstance().initDb();
        Toast.makeText(this, "更新数据失败，请登录后手动更新", Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoading() {
        mDialog = new ProgressDialog(this);
        mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setCancelable(false);
        mDialog.setMessage("正在下载，请稍后");
        mDialog.show();
    }

    @Override
    public void stopLoading() {
        if(mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        float r = (float) arg;
        if(mDialog != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mDialog.setProgress((int) r);
                }
            });
        }
    }
}